package com.leruan.aasimp.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.dao.MaintainItemsDao;
import com.leruan.aasimp.entity.CarMaintenanceItem;

public class MaintainItemsDaoImpl extends HibernateDaoSupport implements MaintainItemsDao{

	public void addMtItems(List<CarMaintenanceItem> items) {
		Session session = this.getSession();
		for(CarMaintenanceItem item: items){
			session.save(item);
		}
	}

	@SuppressWarnings("unchecked")
	public List<CarMaintenanceItem> getItemsById(Long maintainId) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarMaintenanceItem.class);
		Criterion  c = Expression.eq("maintainId", maintainId);
		cri.add(c);
		List<CarMaintenanceItem> list = cri.list();
		return list;
		
	}
	
	public void deleteItems(Long maintainId) {
		Session session = this.getSession();
		List<CarMaintenanceItem> list = getItemsById(maintainId);
		for(CarMaintenanceItem item : list){
			session.delete(item);
		}
	}

	@SuppressWarnings("unchecked")
	public List<CarMaintenanceItem> getAllMtParts() {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarMaintenanceItem.class);
		List<CarMaintenanceItem> list = cri.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Long> getAllMaintainIds() {
		Session session = this.getSession();
		String hql = "select maintainId from CarMaintenanceItem group by maintainId";
		List<Long> list = session.createQuery(hql).list();
		return list;
	}
}
